const App = getApp();
import { url } from '../../modelApi/url_config.js'
let statusBarHeight = getApp().globalData.navHeight + 10;
Page({
    data: {
        statusBarHeight: statusBarHeight,
        orderDetail: {},
        orderTipObj: {
          "0": "待付款",
          "1": "待发货",
          "2": "待收货",
          "3": "待评价",
          "4": "已取消",
          "9": "已完成"
        },
        payStatusTip:"",
        addressId: '',
    },
    onLoad: function (options) {
        this.getOrderInfoFunc(options.orderId);
        this.setData({
            orderId: options.orderId
        })
    },
    getaddrpageAddr(id) {
        this.setData({
            addressId: id
        })
    },
    goAddressList: function () {
        if (this.data.addressId) {
            wx.navigateTo({
                url: "/pages/address/index?addressId=" + this.data.addressId
            });
        } else {
            wx.navigateTo({
                url: "/pages/address/index"
            });
        }
    },
    getOrderInfoFunc(orderId) {
        const param = { orderId: orderId }
        App.ajax(url.getorderDetial, param, 'get').then(res => {
            if (res.code === 0 && res.data) {
                this.setData({
                    orderDetail: res.data,
                    payStatusTip: this.data.orderTipObj[res.data.orderStatus]
                })
            }
        })
    },
    /* 微信支付 */
    gopay: function () {
        const that = this;
        let params = {
            openId: wx.getStorageSync('openId'),
            orderId: this.data.orderId
        }
        App.ajax(url.wxapiPayUrl, params, 'post').then(res => {
            if (res.code === 0 && res.data) {
                that.setData({
                    out_trade_no: res.data.out_trade_no
                })
                wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': res.data.signType,
                    'paySign': res.data.paySign,
                    success: function (res1) {
                        that.getorderNo(that.data.out_trade_no);
                    },
                    fail: function (res1) {
                        wx.showToast({
                            title: '取消支付',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                })
            }
        })
    },
    // 获取轮循订单号
    getorderNo(data) {
        const param = { orderNo: data }
        App.ajax(url.getByNo, param, 'get').then(res => {
            const data = res.data;
            if (+res.code === 0 && data) {
                if (+data.payStatus === 1) {
                    wx.reLaunch({
                        url: `/pages/orderDetail/index?orderId=${data.orderId}`,
                    })
                    return;
                }
                this.getorderNo(this.data.out_trade_no)
            }
        })
    },
})